 
 dim GameFlags = a
 dim JumpCounter = b
 dim JumpSpeed = c
 dim WallJump = d
 dim SawEndOne = e
 dim SawEndTwo = f
 dim SawDirection = g
 dim MissileCounter = h
 dim Missile0xhome = i
 dim Missile0yhome = j
 dim Missile0dir = k
 dim Missile1xhome = l
 dim Missile1yhome = m
 dim Missile1dir = n

 dim MeatDeaths = u
 dim BandageAnimationCounter = w 
 dim MeatAnimationCounter = v 
 dim ScreenX = x
 dim ScreenY = y
 dim GameScreen = z
 
 def IsJumping=a{0}
 def IsFalling=a{1}
 def WallSlide=a{2}
 def WallSlideSpr=a{3}
 def JumpHold=a{4}
 def MeatBoyFacing=a{6}
 def BandageGirlFrame=a{7}
 
 set smartbranching on
 set legacy .99
 include div_mul.asm
 
 const Jump1st = 6
 const Jump2nd = 14
 const JumpMidd =17
 const Jump3rd = 20
 const Jump4th = 28
 
 BandageAnimationCounter = 0
 
titlescreen

 COLUBK = $00
 CTRLPF = $31
 missile0height = 1
 missile1height = 1
 if BandageAnimationCounter < 30 then COLUPF = $42 : BandageGirlFrame = 0
 if BandageAnimationCounter >= 30 then COLUPF = $46 : BandageGirlFrame = 1
 if BandageAnimationCounter = 60 then BandageAnimationCounter =0
 BandageAnimationCounter = BandageAnimationCounter + 1
 GameScreen = 0
 
 
 if BandageGirlFrame then gosub bandagegirl1
 if !BandageGirlFrame then gosub bandagegirl0
 
 if joy0fire then JumpHold = 1 else JumpHold = 0
 
 scorecolor = $00
 AUDV0 = 0 : AUDV1 = 0
 player0x=40
 player0y=80
 player1x=143
 player1y=80

 
 playfield:
 .........X.X..XXX..X..XXX.......
 ........X.X.X.X.X.X.X..X........
 ........X.X.X.XX..XXX..X........
 ........X.X.X.XXX.X.X..X........
 ................................
 ...........X...XXX.X.X..........
 ...........XXX.X.X.X.X..........
 ...........X.X.X.X.XXX..........
 ...........XXX.XXX..X...........
 ................................
 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
end

 COLUP0 = $40
 COLUP1 = $4E
 

 drawscreen
 
 gosub meatboyRstand
 
 
 if !joy0fire && !JumpHold then titlescreen
 
 ScreenX = 0
 ScreenY = 0
 GameScreen = 1
 
 
 rem --------------------------------------------------------------------------
 rem                             INITIALIZATION
 rem --------------------------------------------------------------------------
Initialize
 scorecolor = $0E
 COLUPF = $80
 MissileCounter = 0
 missile0y = 100
 missile1y = 100
 if GameScreen = 1 then gosub loadScreenOne
 if GameScreen = 2 then gosub loadScreenTwo
 if GameScreen = 3 then gosub loadScreenThree
 if GameScreen = 4 then gosub loadScreenFour
 if GameScreen = 5 then BandageAnimationCounter = 0 : goto winloop
 
 gosub bandagegirl0
 MeatAnimationCounter = 0
 IsFalling = 0
 JumpCounter = 0
 BandageGirlFrame = 0
 gosub meatboyDie2
 
 rem------------------------------------------------------------------
 rem                                MEAT SPAWN LOOP
 rem------------------------------------------------------------------
spawnloop
 
  COLUP0 = $40
  COLUP1 = $4E
   
 if MeatAnimationCounter = 5 then gosub meatboyDie1
 if MeatAnimationCounter = 10 then gosub meatboyDie0
 if MeatAnimationCounter = 15 then gosub meatboyRstand
 
 MeatAnimationCounter = MeatAnimationCounter + 1
 
 drawscreen
 
 if MeatAnimationCounter < 20 goto spawnloop
 
 MeatAnimationCounter = 0
 
 rem---------------------------------------------------------------------------
 rem                               MAIN LOOP
 rem---------------------------------------------------------------------------
mainloop
 
 COLUP0 = $40
 COLUP1 = $4E
 if joy0fire then JumpHold = 1 else JumpHold = 0
 
 if BandageAnimationCounter < 30 then BandageGirlFrame = 0
 if BandageAnimationCounter >= 30 then BandageGirlFrame = 1
 if BandageAnimationCounter = 60 then BandageAnimationCounter =0
 BandageAnimationCounter = BandageAnimationCounter + 1
 
 if BandageGirlFrame then gosub bandagegirl1
 if !BandageGirlFrame then gosub bandagegirl0
 
 ScreenX = (player0x-31)/4
 ScreenY = (player0y-8)/8
 WallSlide = 0

 temp4 =  (player0y-1)/8
 rem ===== GO LEFT =====
 
 temp5 = (player0x-32)/4
 if pfread(temp5,ScreenY) then WallSlide = 1 : WallJump = 0 : goto Skip_Joy0Left
 if pfread(temp5,temp4) then WallSlide = 1 : WallJump = 0 : goto Skip_Joy0Left
 if joy0left && !joy0right && WallJump = 0 then player0x = player0x-1 : MeatBoyFacing = 1

   
 if WallJump > 0 && MeatBoyFacing then player0x = player0x-1

Skip_Joy0Left

 rem ===== GO RIGHT =====

 temp5 = (player0x-23)/4
 if pfread(temp5,ScreenY) then WallSlide = 1 : WallJump = 0 : goto Skip_Joy0Right
 if pfread(temp5,temp4) then WallSlide = 1 : WallJump = 0 : goto Skip_Joy0Right 
 if joy0right && !joy0left && WallJump = 0 then player0x = player0x+1 : MeatBoyFacing = 0
 
 
 if WallJump > 0 && !MeatBoyFacing then player0x = player0x + 1
 
Skip_Joy0Right
 


 rem ===== JUMP CODE =====
 temp4 =  (player0x-24)/4
 temp5 = (player0y-9)/8
 if IsFalling then goto SkipJump_2
 if pfread(ScreenX,temp5) then goto SkipJump_1
 if pfread(temp4,temp5) then goto SkipJump_1
 if !joy0fire && IsJumping then SkipJump_1
 if joy0fire && !IsJumping then JumpCounter = 0 : JumpSpeed = 2
 if JumpCounter = JumpMidd then goto SkipJump_1
 if joy0fire then player0y = player0y-JumpSpeed : IsJumping = 1 : JumpCounter = JumpCounter + 1 : goto SkipJump_2
 
 
SkipJump_1
 IsFalling = 1 : IsJumping = 0 : JumpCounter = JumpMidd : JumpSpeed = 0
SkipJump_2




 rem ===== FALL CODE =====
 temp5 = (player0y)/8
 if pfread(ScreenX,temp5) then goto SkipFall_1
 if pfread(temp4,temp5) then goto SkipFall_1
 if IsJumping then goto SkipFall_2
 if !IsFalling then JumpCounter = JumpMidd
 if !joy0up then player0y = player0y + JumpSpeed : JumpCounter = JumpCounter + 1 : IsFalling = 1 : goto SkipFall_2

SkipFall_1
 IsFalling = 0
 JumpCounter = 0

SkipFall_2
 
 

 
 temp5 = (player0y-9)/8
 if pfread(ScreenX,temp5) then goto SkipUp
 if pfread(temp4,temp5) then goto SkipUp
 
 rem if joy0up && !joy0down then player0y = player0y-1
 
SkipUp
 
 
 gosub AnimateMeatBoy
 gosub MoveSaw
 if MissileCounter > 0 then MissileCounter = MissileCounter - 1
 if MissileCounter = 1 then gosub FireMissile
 
 drawscreen
 
 rem ===== WALL JUMP CODE =====
 if WallSlide && !JumpHold && IsFalling && joy0fire && joy0left then MeatBoyFacing = 0 : WallJump = 8 : player0x = player0x + 1: IsFalling = 0
 if WallSlide && !JumpHold && IsFalling && joy0fire && joy0right then MeatBoyFacing = 1 : WallJump = 8 : player0x = player0x - 1:IsFalling = 0


 if WallJump > 0 then WallJump = WallJump - 1
 
 
 
 
 if JumpCounter = 0 then goto SkipGravity
 
 if JumpCounter = Jump1st then JumpSpeed = 1
 if JumpCounter = Jump2nd then JumpSpeed = 0
 if JumpCounter = Jump3rd then JumpSpeed = 1
 if JumpCounter = Jump4th then JumpSpeed = 2
  
SkipGravity
 
 if collision(player0,playfield) then player0y = player0y - 1
 if collision(player0, player1) then GameScreen = GameScreen + 1 : MeatAnimationCounter = 0 : goto getBandageLoop
 if collision(player0, ball) then MeatAnimationCounter = 0 : MeatDeaths = MeatDeaths + 1: goto dieloop
 if collision(player0, missile0) then MeatAnimationCounter = 0 : MeatDeaths = MeatDeaths + 1: goto dieloop
 if collision(player0, missile1) then MeatAnimationCounter = 0 : MeatDeaths = MeatDeaths + 1: goto dieloop
 if player0y > 88 then MeatAnimationCounter = 0  : MeatDeaths = MeatDeaths + 1: goto dieloop
 if player0x < 27 then MeatAnimationCounter = 0 : MeatDeaths = MeatDeaths + 1: goto dieloop
 if player0x > 156 then MeatAnimationCounter = 0 : MeatDeaths = MeatDeaths + 1: goto dieloop
   
 goto mainloop
 

 rem------------------------------------------------------------------
 rem                                DIE LOOP
 rem------------------------------------------------------------------
dieloop
 
  COLUP0 = $40
  COLUP1 = $4E
 score = 0
   if MeatDeaths > 0 then for temp6 = 1 to MeatDeaths : score = score + 1 : next
 if MeatAnimationCounter = 5 then gosub meatboyDie0
 if MeatAnimationCounter = 10 then gosub meatboyDie1
 if MeatAnimationCounter = 15 then gosub meatboyDie2
 
 MeatAnimationCounter = MeatAnimationCounter + 1
 
 drawscreen
 
 if MeatAnimationCounter > 19 then goto Initialize else goto dieloop

 
 rem------------------------------------------------------------------
 rem                          GET BANDAGE LOOP
 rem------------------------------------------------------------------
 
getBandageLoop

 COLUP0 = $40
 COLUP1 = $4E
 
 MeatAnimationCounter = MeatAnimationCounter + 1
 
 drawscreen
 gosub bandagegirlwin
 if MeatAnimationCounter > 19 then goto Initialize else goto getBandageLoop
 
 
 rem------------------------------------------------------------------
 rem                                WIN LOOP
 rem------------------------------------------------------------------
winloop

 player0x=123
 player0y=48
 player1x=131
 player1y=48
 COLUP0 = $40
 COLUP1 = $4E
  if BandageAnimationCounter < 30 then COLUPF = $42 : BandageGirlFrame = 0
 if BandageAnimationCounter >= 30 then COLUPF = $46 : BandageGirlFrame = 1
 if BandageAnimationCounter = 60 then BandageAnimationCounter =0
 BandageAnimationCounter = BandageAnimationCounter + 1
 GameScreen = 0
 
 bally = 100
 
 playfield:
 ....X.X.XXX.X.X.................
 ....X.X.X.X.X.X.................
 .....X..X.X.X.X.................
 .....X..XXX.XXX.................
 ................................
 ..X.X.X.XXX.XX..X...............
 ..X.X.X..X..XXX.X.....XXXXXX....
 ..X.X.X..X..X.XXX......XXXX.....
 ...XXX..XXX.X..XX......XXXX.....
 ......................XXXXXX....
 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
end


 drawscreen
 
 
 gosub meatboyRwin
 gosub bandagegirlwin

 
 if !joy0fire then winloop
 
 
 
 
 goto titlescreen
 
 
 
 
 
 
 rem---------------------------------------------------------------------------
 rem                               SUB ROUTINE METHODS
 rem---------------------------------------------------------------------------

 
loadScreenOne
 COLUPF = $06

  playfield:
 X..............................X
 X..............................X
 X..............................X
 X..............................X
 X..............................X
 X........XXXXXXXXXXXXXX........X
 X..........XXXXXXXXXX..........X
 X..............................X
 X..............................X
 XXXX........................XXXX
 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
end
 player0x=92
 player0y=80
 player1x=92
 player1y=40
 ballx = 120
 bally = 100
 ballheight = 8
 MeatBoyFacing = 0

 return
 
 
loadScreenTwo
 COLUPF = $06
 playfield:
 XXXXXXXXXXXXXXXXXXXXXX......XXXX
 XXXXXXXXXXX.................XXXX
 XXXXXXXXXXX.................XXXX
 XXXXXXXXXXX.................XXXX
 XXXXXXXXXXXXXXXXXXXXXX......XXXX
 XXXXXXXXXXXXXXXXXXXXXX......XXXX
 XXXXXX..........XXXXXX......XXXX
 XX..........................XXXX
 XX..........................XXXX
 XX..........................XXXX
 XXXXXXXX......XXXXXXXX......XXXX
end


 player0x=48
 player0y=80
 player1x=92
 player1y=32
 SawEndOne = 15
 SawEndTwo = 80
 SawDirection = 1
 ballx = 112
 bally = 80
 ballheight = 7
 MeatBoyFacing = 0
 return
 
loadScreenThree
 COLUPF = $06
 playfield:
 XXXXXXXXXXXXXXXXXXXXXX..........
 XXXXXXXXXXXXXXXXXX..............
 XXXXXXXXXX......................
 XXXXXX..........................
 ............................XXXX
 ............................XXXX
 ......................XXXXXXXXXX
 ..................XXXXXXXXXXXXXX
 ..........XXXXXXXXXXXXXXXXXXXXXX
 ......XXXXXXXXXXXXXXXXXXXXXXXXXX
 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
end


 player0x=40
 player0y=80
 player1x=147
 player1y=32
 SawEndOne = 23
 SawEndTwo = 64
 SawDirection = 3
 ballx = 92
 bally = 24
 Missile0xhome = 108	
 Missile0yhome = 60
 Missile0dir = 1
 Missile1xhome = 144
 Missile1yhome = 44
 Missile1dir = 1
 MissileCounter = 2
 MeatBoyFacing = 0
 missile0height = 2
 missile1height = 2
 
 return
 
loadScreenFour
 COLUPF = $06
 playfield:
 ................................
 ................................
 XX.............XX.............XX
 XX............................XX
 XXXXXXXXXXXX........XXXXXXXXXXXX
 XXXXXXXXXXXX........XXXXXXXXXXXX
 XXXXXXXXXXXX........XXXXXXXXXXXX
 XXXX..XX..XX..........XX..XX..XX
 XX............................XX
 XX............................XX
 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
end


 player0x=143
 player0y=80
 player1x=91
 player1y=16
 SawEndOne = 24
 SawEndTwo = 86
 SawDirection = 3
 ballx = 92
 bally = 24
 Missile0xhome = 40	
 Missile0yhome = 76
 Missile0dir = 0
 Missile1xhome = 156
 Missile1yhome = 28
 Missile1dir = 1
 MissileCounter = 2
 MeatBoyFacing = 1
 missile0height = 2
 missile1height = 2
 
 return
 
loadScreenFive
 COLUPF = $06
 playfield:
 XXXXXXXXXXXXXXXXXXXXXX..........
 XXXXXXXXXXXXXXXXXX..............
 XXXXXXXXXX......................
 XXXXXX..........................
 ............................XXXX
 ............................XXXX
 ......................XXXXXXXXXX
 ..................XXXXXXXXXXXXXX
 ..........XXXXXXXXXXXXXXXXXXXXXX
 ......XXXXXXXXXXXXXXXXXXXXXXXXXX
 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
end

 player0x=40
 player0y=80
 player1x=147
 player1y=32
 SawEndOne = 23
 SawEndTwo = 64
 SawDirection = 3
 ballx = 92
 bally = 24
 Missile0xhome = 108	
 Missile0yhome = 60
 Missile0dir = 1
 Missile1xhome = 144
 Missile1yhome = 44
 Missile1dir = 1
 MissileCounter = 2
 MeatBoyFacing = 0
 missile0height = 2
 missile1height = 2

 
 return
 
 rem ------------------------------------------------------------------
 rem                        ANIMATE MEAT BOAI
 rem ------------------------------------------------------------------
AnimateMeatBoy

 MeatAnimationCounter = MeatAnimationCounter + 1
 
 if MeatBoyFacing then goto SkipAnimRight
 
 if WallSlide then gosub meatboyRslide : MeatAnimationCounter = 3
 if !joy0right && !joy0left then MeatAnimationCounter = 0 : gosub meatboyRstand : return
 if MeatAnimationCounter = 4 then gosub meatboyRwalk1
 if MeatAnimationCounter = 8 then gosub meatboyRwalk0 : MeatAnimationCounter = 0 
 
 return

SkipAnimRight

 if WallSlide then gosub meatboyLslide : MeatAnimationCounter = 3
 if !joy0right && !joy0left then MeatAnimationCounter = 0 : gosub meatboyLstand : return
 if MeatAnimationCounter = 4 then gosub meatboyLwalk1
 if MeatAnimationCounter = 8 then gosub meatboyLwalk0 : MeatAnimationCounter = 0
 
 return
 
 rem-------------------------------------------------------------------
 rem                           SAW BLADE MOVE
 rem-------------------------------------------------------------------

MoveSaw

 if missile0x < 32 then missile0y = 100
 if missile1x < 32 then missile1y = 100
 if missile0x > 156 then missile0y = 100
 if missile1x > 156 then missile1y = 100
 if Missile0dir = 1 then missile0x = missile0x - 2 else missile0x = missile0x + 2
 if Missile1dir = 1 then missile1x = missile1x - 2 else missile1x = missile1x + 2

 if bally = 100 then return 
branch on SawDirection goto 10 11 12 13
10 if ballx < SawEndTwo then ballx = ballx + 1 else SawDirection = 2 : return
 return
11 if bally > SawEndOne then bally = bally - 1 else SawDirection = 3 : return
 return
12 if ballx > SawEndOne then ballx = ballx - 1 else SawDirection = 0 : return
 return
13 if bally < SawEndTwo then bally = bally + 1 else SawDirection = 1 
 return
 
 rem ------------------------------------------------------------------
 rem                                FIRE MISSILE
 rem ------------------------------------------------------------------
FireMissile

 missile0x = Missile0xhome
 missile0y = Missile0yhome
 
 missile1x = Missile1xhome
 missile1y = Missile1yhome
 
 MissileCounter = 101
 
 return
 rem--------------------------------------------------------------------------- 
 rem                           SPRITE ANIMATIONS METHODS
 rem---------------------------------------------------------------------------


meatboyRstand
 player0:
 %01100110
 %11111111
 %11111111
 %11111111
 %11001001
 %11001001
 %11111111
 %11111111
end
 return
 
meatboyRwalk0
 player0:
 %00111100
 %11111111
 %11111111
 %11111111
 %11100100
 %11100100
 %11111111
 %11111111
end
 return
 
meatboyRwalk1
 player0:
 %01100110
 %11111111
 %11111111
 %11111111
 %11100100
 %11100100
 %11111111
 %11111111
end
 return
 
meatboyLstand
 player0:
 %01100110
 %11111111
 %11111111
 %11111111
 %10010011
 %10010011
 %11111111
 %11111111
end
 return
 
meatboyLwalk0
 player0:
 %00111100
 %11111111
 %11111111
 %11111111
 %00100111
 %00100111
 %11111111
 %11111111
end
 return
 
meatboyLwalk1
 player0:
 %01100110
 %11111111
 %11111111
 %11111111
 %00100111
 %00100111
 %11111111
 %11111111
end
 return
 
meatboyRslide
 player0:
 %00011011
 %01111111
 %01111111
 %01111111
 %00010011
 %00111011
 %01111111
 %01111111
end
 return

meatboyLslide
 player0:
 %11011000
 %11111110
 %11111110
 %11111110
 %11001000
 %11011100
 %11111110
 %11111110
end
 return

meatboyDie0
 player0:
 %01100110
 %11110111
 %11101111
 %11100011
 %01000100
 %10110111
 %11110111
 %11101111
end
 return

meatboyDie1
 player0:
 %11000011
 %01100111
 %10101100
 %11001111
 %00000000
 %01100111
 %10101100
 %00100101
end
 return
 
meatboyDie2
 player0:
 %01000010
 %00100101
 %00000000
 %10000000
 %00000000
 %01000001
 %10001000
 %00100100
end
 return

meatboyRwin
 player0:
 %01100110
 %11111111
 %11100111
 %11011111
 %11111111
 %10101010
 %11011101
 %11111111
end 
 return
 
bandagegirl0
 player1:
 %01100110
 %11111111
 %11111111
 %11111111
 %10010011
 %10010011
 %11111111
 %00000000
end
 return
 
bandagegirl1
 player1:
 %01100110
 %11111111
 %11100111
 %11111111
 %10010011
 %10010011
 %11111111
 %11000011
end
 return
 
bandagegirlwin
 player1:
 %01111110
 %11100111
 %11000011
 %11111111
 %01010101
 %10111011
 %11111111
 %11000011
end
 return

 
 